package com.douqu.game.main.msg;

import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.database.model.PlayerModel;
import com.douqu.game.core.e.E_MailSenderType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.entity.db.MailDB;
import com.douqu.game.core.entity.db.PrivacyDB;
import com.douqu.game.core.entity.ext.BagInfo;
import com.douqu.game.core.entity.ext.MailInfo;
import com.douqu.game.core.entity.ext.SettingInfo;
import com.douqu.game.core.entity.ext.data.mail.DialogueData;
import com.douqu.game.core.entity.world.WorldOfficialBean;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WordFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.protobuf.SGMainProto;
import com.douqu.game.core.protobuf.SGPlayerProto;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.SendUtils;
import com.douqu.game.core.util.StringUtils;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.main.GameServer;
import com.douqu.game.main.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: Gavin.
 * Description:
 * Date: 2017/12/11 0011 下午 2:41
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
@Component
public class MailMsgChannel implements AMsgChannel  {


    @Autowired
    private PlayerService playerService;


    @Override
    public void messageChannel(int code, PlayerController playerController, byte[] data) throws Exception {
        if(playerController == null)
            throw new Exception("playerController is null");
        MailInfo mailInfo = playerController.getPlayer().getExtInfo(MailInfo.class);

        switch (code) {
            case SGMainProto.E_MSG_ID.MsgID_Mail_List_VALUE:
                //邮件列表显示
                SGPlayerProto.C2S_MailList c = SGPlayerProto.C2S_MailList.parseFrom(data);
                getMailList(playerController, c.getMailTypeValue(), mailInfo);
                break;
            case SGMainProto.E_MSG_ID.MsgID_Mail_Detail_VALUE:
                //读取邮件
                SGPlayerProto.C2S_MailDetail detail = SGPlayerProto.C2S_MailDetail.parseFrom(data);
                mailDetail(playerController, detail.getMailTypeValue(), detail.getMailId(), mailInfo);
                break;
            case SGMainProto.E_MSG_ID.MsgID_Mail_GetAuxiliary_VALUE:
                //领取附件
                SGPlayerProto.C2S_MailGetAuxiliary auxiliary = SGPlayerProto.C2S_MailGetAuxiliary.parseFrom(data);
                getAuxiliaryList(playerController,auxiliary.getMailId(),mailInfo);
                break;
            case SGMainProto.E_MSG_ID.MsgID_Mail_PrivacyMailList_VALUE:
                //私信列表
                SGPlayerProto.C2S_MailPrivacyMailList privacyList = SGPlayerProto.C2S_MailPrivacyMailList.parseFrom(data);
                getPrivacyList(privacyList.getMailTypeValue(), mailInfo, playerController);
                break;
            case SGMainProto.E_MSG_ID.MsgID_Mail_PrivacyMailDetail_VALUE:
                //私信详情
                SGPlayerProto.C2S_MailPrivacyMailDetail privacyDetail = SGPlayerProto.C2S_MailPrivacyMailDetail.parseFrom(data);
                getPrivacyDetail(privacyDetail.getPrivacyMailId(),mailInfo,playerController);
                break;
            case SGMainProto.E_MSG_ID.MsgID_Mail_SendPrivacyMail_VALUE:
                SGPlayerProto.C2S_MailSendPrivacyMail msp = SGPlayerProto.C2S_MailSendPrivacyMail.parseFrom(data);
                sendPrivacy(playerController,msp,mailInfo);
                break;
            case SGMainProto.E_MSG_ID.MsgID_Mail_BlackList_VALUE:
                //黑名单列表
                blackList(playerController);
                break;
            case SGMainProto.E_MSG_ID.MsgID_Mail_AddAndRemoveBlackList_VALUE:
                //添加移除黑名单
                SGPlayerProto.C2S_MailAddAndRemoveBlackList bl = SGPlayerProto.C2S_MailAddAndRemoveBlackList.parseFrom(data);
                addAndRemoveBlackList(playerController,bl.getTargetIndex(),bl.getTypeValue());
                break;
            case SGMainProto.E_MSG_ID.MsgID_Mail_CheckSendJurisdiction_VALUE:
                //检测是否有权限
                checkSendAll(playerController);
                break;
            default:
                break;
        }
    }

    /***
     * 获取邮件列表
     * @param type
     */
    public void getMailList(PlayerController playerController,int type,MailInfo mailInfo)
    {
        List<MailDB> lists = null;
        //获取某种类型的邮件集合
        SGPlayerProto.S2C_MailList.Builder b =  SGPlayerProto.S2C_MailList.newBuilder();
        b.setMailType(SGCommonProto.E_MAIL_TYPE.forNumber(type));
        if(mailInfo.mailMap != null && mailInfo.mailMap.size() > 0) {
            lists = mailInfo.mailMap.get(type);
            SGCommonProto.Mail.Builder mail = null;
            if(lists != null && lists.size() > 0){
                for (MailDB maildb : lists) {
                    //1.标题,2.内容，3.发送者，4是否已读，附件
                    mail = SGCommonProto.Mail.newBuilder();
                    mail.setId(maildb.id);
                    mail.setTitle(maildb.title);
                    mail.setIsGet(maildb.isGet);
                    mail.setSender(maildb.target);
                    mail.setSendTime(maildb.sendTime);
                    mail.setIsRead(maildb.isRead);
                    //附件
                    getAuxiliary(type,mail,maildb);

                    b.addMail(mail);
                }
            }
        }
        b.addAllRedPointList(mailInfo.getMailReadPoint());
        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Mail_List_VALUE, b.build().toByteArray());
    }


    /***
     * 邮件详情 设置已读邮件
     * @param playerController
     * @param type
     * @param mailId
     */
    public void mailDetail(PlayerController playerController, int type, int mailId ,MailInfo mailInfo){
        MailDB mailDB = mailInfo.getMail(type, mailId);
        if(mailDB == null){
            //邮件不存在
            playerController.sendWarn(WordFactory.MAIL_NOT_EXIST, mailDB.id);
            return;
        }

        mailDB.isRead = true;

        SGPlayerProto.S2C_MailDetail.Builder b = SGPlayerProto.S2C_MailDetail.newBuilder();
        SGCommonProto.Mail.Builder mail  = SGCommonProto.Mail.newBuilder();
        mail.setId(mailDB.id);
        mail.setTitle(mailDB.title);
        mail.setIsGet(mailDB.isGet);
        mail.setSender(mailDB.target);
        mail.setSendTime(mailDB.sendTime);
        mail.setIsRead(mailDB.isRead);
        mail.setContent(mailDB.content);
        getAuxiliary(type, mail, mailDB);
        b.setMail(mail);
        b.setMailType(SGCommonProto.E_MAIL_TYPE.forNumber(type));
        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Mail_Detail_VALUE, b.build().toByteArray());
    }


    //附件
    public void getAuxiliary(int type,SGCommonProto.Mail.Builder mail,MailDB mailDB){
        SGCommonProto.FlushData.Builder goodsGet = null;
        //系统邮件
        if(type == SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM_VALUE){
            for (GoodsData goodsData : mailDB.getAttachList()) {
                goodsGet = SGCommonProto.FlushData.newBuilder();
                goodsGet.setId(goodsData.id);
                goodsGet.setType(SGCommonProto.E_GOODS_TYPE.forNumber(goodsData.type));
                goodsGet.setChange(goodsData.value);
                mail.addExtGoods(goodsGet);
            }
        }
    }


    /***
     * 私信列表
     * @param type
     * @param mailInfo
     * @param playerController
     */
    public void getPrivacyList(int type,MailInfo mailInfo,PlayerController playerController){
        SGPlayerProto.S2C_MailPrivacyMailList.Builder b = SGPlayerProto.S2C_MailPrivacyMailList.newBuilder();
        SGCommonProto.PrivacyMail.Builder privacy = null;
        if (type != SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_PRIVACY_VALUE) {
            playerController.sendWarn(WordFactory.PARAM_ERROR);
            return;
        }
        PlayerController targetController = null;
        for (PrivacyDB privacyDB : mailInfo.privacyList) {
            //私信
            privacy = SGCommonProto.PrivacyMail.newBuilder();
            privacy.setPrivacyMailId(privacyDB.id);
            privacy.setSendTime(privacyDB.sendTime);
            privacy.setTargetIndex(privacyDB.targetIndex);
            privacy.setIsRead(privacyDB.isRead);
            privacy.setContent(privacyDB.content);
            //头像
            targetController = GameServer.getInstance().getWorldManager().getPlayerController(privacyDB.targetIndex);
            if(targetController != null)
            {
                privacy.setTargetAvatar(targetController.getPlayer().avatar);
                privacy.setNickName(targetController.getPlayer().name);
            }
            else
            {
                Map<String, Object> targetInfo = GameServer.getInstance().getWorldManager().getNameAvatarInfo(privacyDB.targetIndex);
                if(targetInfo != null)
                {
                    privacy.setTargetAvatar(targetInfo.get("avatar") != null ? targetInfo.get("avatar").toString() : "");
                    privacy.setNickName(targetInfo.get("name") != null ? targetInfo.get("name").toString() : "");
                }
            }

            b.addPrivacy(privacy);
        }
        b.addAllRedPointList(mailInfo.getMailReadPoint());
        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Mail_PrivacyMailList_VALUE, b.build().toByteArray());
    }


    /***
     * 私信详情
     * @param mailInfo
     * @param playerController
     */
    public void getPrivacyDetail(int privacyId,MailInfo mailInfo,PlayerController playerController){
        SGPlayerProto.S2C_MailPrivacyMailDetail.Builder b = SGPlayerProto.S2C_MailPrivacyMailDetail.newBuilder();
        SGCommonProto.PrivacyMail.Builder  privacy = SGCommonProto.PrivacyMail.newBuilder();
        SGCommonProto.Dialogue.Builder dialogue = null;
        PlayerController targetController = null;
        for (PrivacyDB privacyDB : mailInfo.privacyList)
        {
            if(privacyId == privacyDB.id){
                if(!privacyDB.isRead)
                    privacyDB.isRead = true;
                for (DialogueData dialogueData : privacyDB.replyList) {
                    dialogue = SGCommonProto.Dialogue.newBuilder();
                    dialogue.setSendTime(dialogueData.sendTime);
                    if(StringUtils.isNullOrEmpty(dialogueData.targetIndex)){
                        dialogue.setTargetIndex("");
                        dialogue.setAvatar("");
                    }else{
                        dialogue.setTargetIndex(dialogueData.targetIndex);
                        targetController = GameServer.getInstance().getWorldManager().getPlayerController(dialogueData.targetIndex);
                        if(targetController != null)
                        {
                            dialogue.setAvatar(targetController.getPlayer().avatar);
                        }
                        else
                        {
                            Map<String, Object> targetInfo = GameServer.getInstance().getWorldManager().getNameAvatarInfo(dialogueData.targetIndex);
                            if(targetInfo != null)
                            {
                                dialogue.setAvatar(targetInfo.get("avatar") != null ? targetInfo.get("avatar").toString() : "");
                            }
                        }
                    }
                    dialogue.setContent(dialogueData.content);
                    dialogue.setMailSendType(SGCommonProto.E_MAIL_SENDER_TYPE.forNumber(dialogueData.senderType.getCode()));
                    privacy.addDialogue(dialogue);
                }
                privacy.setPrivacyMailId(privacyDB.id);
                b.setPrivacyMail(privacy);
                break;
            }

        }
        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Mail_PrivacyMailDetail_VALUE, b.build().toByteArray());
    }


    /***
     * 领取附件
     * @param playerController
     * @param mailId
     */
    public void getAuxiliaryList(PlayerController playerController,int mailId,MailInfo mailInfo) {

        SGPlayerProto.S2C_MailGetAuxiliary.Builder b = SGPlayerProto.S2C_MailGetAuxiliary.newBuilder();

        List<MailDB> mailList = null;
        BagInfo bagInfo = playerController.getPlayer().getExtInfo(BagInfo.class);
        //单个领取
        if(mailId > ConstantFactory.ZERO){
            MailDB mailDB = mailInfo.getMail(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM_VALUE, mailId);
            if(mailDB == null){
                //邮件不存在
                playerController.sendWarn(WordFactory.MAIL_NOT_EXIST, mailDB.id);
                return;
            }
            if(mailDB.isGet){
                //此邮件的奖励已领取不能再次领取
                playerController.sendWarn(WordFactory.ANNEX_HAS_BEEN_RECEIVED, mailDB.id);
                return;
            }
            mailDB.isGet = true;
            setReadMail(mailDB, bagInfo);
            b.setMailId(mailId);
        }else{
            //一键领取
            mailList = mailInfo.mailMap.get(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM_VALUE);
            if(mailList != null && mailList.size() > 0) {
                for (int i = 0; i < mailList.size(); i++){
                    if(!mailList.get(i).isGet){
                        mailList.get(i).isGet = true;
                        setReadMail(mailList.get(i), bagInfo);
                    }
                }
            }
        }
        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Mail_GetAuxiliary_VALUE, b.build().toByteArray());
    }


    /***
     * 发送回复私信
     * @param playerController
     * @param sendPrivacy
     * @param mailInfo
     * @Param params 如果有值表示不返回response给客户端  没有值表示客户端主要请求的要返回response
     */
    public void sendPrivacy(PlayerController playerController ,SGPlayerProto.C2S_MailSendPrivacyMail sendPrivacy,MailInfo mailInfo,Object... params)
    {
        List<WorldOfficialBean> campsList = null;
        String content = "";
        if(sendPrivacy == null){
            playerController.sendWarn(WordFactory.PARAM_ERROR);
            return;
        }
        if((CoreUtils.isNullOrEmpty(sendPrivacy.getNickName()) && CoreUtils.isNullOrEmpty(sendPrivacy.getTargetIndex())) || sendPrivacy.getNickName().indexOf("GM") != -1 || sendPrivacy.getNickName().indexOf("管理员") != -1)
        {
            LogUtils.error("GM不接受私信 -> " + playerController.getName() + " --> " + sendPrivacy.getNickName());
            playerController.sendWarn(WordFactory.PARAM_ERROR);
            return;
        }
        if(CoreUtils.isNullOrEmpty(sendPrivacy.getContent()))
        {
            playerController.sendWarn(WordFactory.PARAM_ERROR);
            return;
        }

        content = sendPrivacy.getContent();

        TwoTuple<Player, PlayerController> playerResult = null;
        if(sendPrivacy.getPrivacyMailType() == SGCommonProto.E_PRIVACY_MAIL_SENDER_TYPE.MAIL_PRIVACY_MAIL_TYPE_SINGLE){
            if(!StringUtils.isNullOrEmpty(sendPrivacy.getNickName())){
                playerResult = GameServer.getInstance().getWorldManager().getPlayerByName(sendPrivacy.getNickName());
                if(playerResult.getFirst() == null){
                    //用户不存在
                    playerController.sendWarn(WordFactory.USER_DOES_NOT_EXIST);
                    return;
                }
            }else {
                if(sendPrivacy.getPrivacyMailId() <= 0){
                    playerController.sendWarn(WordFactory.PARAM_ERROR);
                    return;
                }
            }
        }else {
            //获取要发送的全员用户列表
            campsList = threeCampsTop5();
        }

        SGPlayerProto.S2C_MailSendPrivacyMail.Builder b =  SGPlayerProto.S2C_MailSendPrivacyMail.newBuilder();
        SGCommonProto.PrivacyMailResponse.Builder r = null;
        long sendTime = DataFactory.currentTime;
        List<Player> offlineList = new ArrayList<>();
        //回复
        if( mailInfo.checkPrivacyIsExists(sendPrivacy.getPrivacyMailId())){
            playerResult = GameServer.getInstance().getWorldManager().getPlayerResult(sendPrivacy.getTargetIndex());
            SettingInfo playerSettingInfo =  playerResult.getFirst().getExtInfo(SettingInfo.class);
            //检测是否在对方的黑名单列表存在，已存在发送，接收方不发送
            if(playerSettingInfo.isContains(playerController.getPlayer().getObjectIndex())){
                //发送方
                SGCommonProto.PrivacyMailResponse.Builder s = mailInfo.sendPrivacyResponse(E_MailSenderType.MAIL_TYPE_SENDER.getCode(),  playerResult.getFirst(), playerController.getPlayer(), content, DataFactory.currentTime, mailInfo);
                b.addPrivacyMailResponseList(s);
            }else{
                b.setPrivacyMailType(sendPrivacy.getPrivacyMailType());
                PrivacyDB replay = new PrivacyDB();
                replay.id = sendPrivacy.getPrivacyMailId();
                replay.sendTime = sendTime;
                replay.content = content;
                DialogueData dialogue = null;
                for (PrivacyDB privacyDB : mailInfo.privacyList){
                    if(privacyDB.id == sendPrivacy.getPrivacyMailId()){
                        r  = SGCommonProto.PrivacyMailResponse.newBuilder();
                        replay.targetIndex = playerController.getObjectIndex();
                        if(!replay.isRead)
                            replay.isRead = true;

                        privacyDB.sendTime = sendTime;
//                    privacyDB.targetIndex = playerController.getObjectIndex();
                        privacyDB.isRead = false;
                        privacyDB.content = content;

                        dialogue = new DialogueData();
                        dialogue.content = content;
                        dialogue.targetIndex = playerController.getObjectIndex();
                        dialogue.sendTime = sendTime;
                        dialogue.senderType = E_MailSenderType.forNumber(E_MailSenderType.MAIL_TYPE_SENDER.getCode());
                        privacyDB.replyList.add(dialogue);
                        replay.replyList = privacyDB.replyList;

                        r.setPrivacyMailId(sendPrivacy.getPrivacyMailId());
                        r.setSendTime(sendTime);
                        r.setContent(content);
                        r.setNickName(playerController.getPlayer().getName());
                        r.setTargetIndex(playerController.getPlayer().getObjectIndex());
                        r.setTargetAvatar(playerController.getPlayer().avatar);
                        r.setIsRead(false);
                        b.addPrivacyMailResponseList(r);
                        break;
                    }
                }

                if(sendPrivacy.getTargetIndex() == null){
                    //参数错误
                    playerController.sendWarn(WordFactory.PARAM_ERROR);
                    return;
                }else {
                    if(sendPrivacy.getTargetIndex().equals(playerController.getObjectIndex())){
                        //参数错误
                        playerController.sendWarn(WordFactory.PARAM_ERROR,sendPrivacy.getTargetIndex());
                        return;
                    }
                }
                //对方
                playerResult = GameServer.getInstance().getWorldManager().getPlayerResult(sendPrivacy.getTargetIndex());
                if(playerResult.getFirst() == null){
                    //用户不存在
                    playerController.sendWarn(WordFactory.USER_DOES_NOT_EXIST);
                    return;
                }
                MailInfo mi =  playerResult.getFirst().getExtInfo(MailInfo.class);
                for (PrivacyDB privacyDB : mi.privacyList){
                    if (privacyDB.targetId != sendPrivacy.getPrivacyMailId() || !playerController.getObjectIndex().equals(privacyDB.targetIndex)){
                        continue;
                    }
                    privacyDB.sendTime = sendTime;
//                privacyDB.targetIndex = playerController.getObjectIndex();
                    privacyDB.isRead = false;
                    privacyDB.content = content;

                    dialogue = new DialogueData();
                    dialogue.targetIndex = playerController.getObjectIndex();
                    dialogue.content = content;
                    dialogue.sendTime = sendTime;
                    dialogue.senderType = E_MailSenderType.forNumber(E_MailSenderType.MAIL_TYPE_RECEIVER.getCode());
                    privacyDB.replyList.add(dialogue);
                    privacyDB.isRead = false;
                    replay.replyList = privacyDB.replyList;
                    break;
                }
                mi.checkRedPointRemindAllCondition(DataFactory.currentTime);
                if(playerResult.getSecond() != null){
                    //推送
                    SendUtils.mailPush(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_PRIVACY, playerResult.getSecond());
                }else {
                    offlineList.add(playerResult.getFirst());
                }
            }
            //发送
        }else{
            PrivacyDB collectPri;
            //循环发送
            if(campsList != null && campsList.size() > 0 ){
                SettingInfo playerSettingInfo = null;
                for (WorldOfficialBean worldOfficialBean :campsList){
                    playerResult = GameServer.getInstance().getWorldManager().getPlayerResult(worldOfficialBean.getObjectIndex());
                    if (playerResult.getFirst() == null){
                        continue;
                    }
                    playerSettingInfo =  playerResult.getFirst().getExtInfo(SettingInfo.class);
                    //检测是否在对方的黑名单列表存在，已存在发送，接收方不发送
                    if(playerSettingInfo.isContains(playerController.getPlayer().getObjectIndex())){
                        //发送方
                        SGCommonProto.PrivacyMailResponse.Builder s = mailInfo.sendPrivacyResponse(E_MailSenderType.MAIL_TYPE_SENDER.getCode(),  playerResult.getFirst(), playerController.getPlayer(), content, DataFactory.currentTime, mailInfo);
                        b.addPrivacyMailResponseList(s);
                    }else{
                        //发送方
                        TwoTuple<SGCommonProto.PrivacyMailResponse.Builder,PrivacyDB> s = mailInfo.sendPrivacyResponseTwo(E_MailSenderType.MAIL_TYPE_SENDER.getCode(),  playerResult.getFirst(), playerController.getPlayer(), content, DataFactory.currentTime, mailInfo);
                        b.addPrivacyMailResponseList(s.getFirst());

                        MailInfo mi =  playerResult.getFirst().getExtInfo(MailInfo.class);
                        //接受方 发送方的邮件id是接收方的targetid  接收方的targetId 是发送方的id
                        collectPri = mi.sendPrivacy(E_MailSenderType.MAIL_TYPE_RECEIVER.getCode(),playerController.getPlayer(), playerResult.getFirst(),content, DataFactory.currentTime,mi,s.getSecond().id);
                        //
                        s.getSecond().targetId = collectPri.id;
                        if(playerResult.getSecond() != null){
                            //推送
                            SendUtils.mailPush(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_PRIVACY, playerResult.getSecond());
                        }else{
                            offlineList.add(playerResult.getFirst());
                        }
                    }
                }
                //单独发送
            }else{

                //获取对方的黑名单列表
                SettingInfo playerSettingInfo = playerResult.getFirst().getExtInfo(SettingInfo.class);
                //检测是否在对方的黑名单列表存在，已存在发送，接收方不发送
                if(playerSettingInfo.isContains(playerController.getObjectIndex())){
                    //发送方
                    SGCommonProto.PrivacyMailResponse.Builder s = mailInfo.sendPrivacyResponse(E_MailSenderType.MAIL_TYPE_SENDER.getCode(), playerResult.getFirst(), playerController.getPlayer(), content, DataFactory.currentTime, mailInfo);
                    b.addPrivacyMailResponseList(s);

                }else {
                    //发送方
                    TwoTuple<SGCommonProto.PrivacyMailResponse.Builder,PrivacyDB> s = mailInfo.sendPrivacyResponseTwo(E_MailSenderType.MAIL_TYPE_SENDER.getCode(),  playerResult.getFirst(), playerController.getPlayer(), content, DataFactory.currentTime, mailInfo);
                    b.addPrivacyMailResponseList(s.getFirst());
                    //接受方
                    MailInfo mi = playerResult.getFirst().getExtInfo(MailInfo.class);
                    collectPri = mi.sendPrivacy(E_MailSenderType.MAIL_TYPE_RECEIVER.getCode(), playerController.getPlayer(), playerResult.getFirst(), content, DataFactory.currentTime, mi,s.getSecond().id);
                    s.getSecond().targetId = collectPri.id;
                    if(playerResult.getSecond() != null){
                        //推送
                        SendUtils.mailPush(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_PRIVACY, playerResult.getSecond());
                    }else{
                        offlineList.add(playerResult.getFirst());
                    }
                }
            }
        }

        GameServer.getInstance().savePlayerList(offlineList, false, MailInfo.class);

        if (params == null || params.length == 0)
            playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Mail_SendPrivacyMail_VALUE, b.build().toByteArray());
    }

    /***
     * 检测是否具有发送全员的权限
     */
    public boolean checkSendJurisdiction(PlayerController playerController){
        List<WorldOfficialBean> list = WorldInfoFactory.getInstance().getWorldOfficialData().getTop5Player(playerController.getPlayer());
        for (WorldOfficialBean worldOfficialBean : list){
            if(worldOfficialBean.getObjectIndex().equals(playerController.getObjectIndex()))
                return true;
            else
                return false;
        }
        return false;
    }

    /***
     * 检测是否具有发送全员的权限
     */
    public void checkSendAll(PlayerController playerController){
        SGPlayerProto.S2C_MailCheckSendJurisdiction.Builder  b =  SGPlayerProto.S2C_MailCheckSendJurisdiction.newBuilder();
        b.setStatus(checkSendJurisdiction(playerController));
        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Mail_CheckSendJurisdiction_VALUE, b.build().toByteArray());
    }

    /***
     * 发送三个阵营前5名玩家
     * @return
     */
    public List<WorldOfficialBean> threeCampsTop5(){
        Map<Integer, Map<Integer,List<WorldOfficialBean>>> officialRanksMap = WorldInfoFactory.getInstance().getWorldOfficialData().getOfficialRanksMap();
        List<WorldOfficialBean> resultList = null;
        for(Map.Entry<Integer,Map<Integer,List<WorldOfficialBean>>> entry : officialRanksMap.entrySet()){
            int camp = entry.getKey();
            WorldOfficialBean rankBean;
            Map<Integer,List<WorldOfficialBean>> idsMap = officialRanksMap.get(camp);
            if(idsMap  != null){
                resultList = new ArrayList<>();
                for(int i = 1 ; i < 6; i++){
                    rankBean = new WorldOfficialBean();
                    rankBean.setRankId(i);
                    List<WorldOfficialBean> list = idsMap.get(i);
                    if(list.size() != 0){
                        rankBean.setObjectIndex(list.get(0).getObjectIndex());
                        rankBean.setName(list.get(0).getName());
                    }
                    resultList.add(rankBean);
                }
            }
        }
        return resultList;
    }

    /***
     * 获取黑名单列表
     * @param playerController
     */
    public void blackList (PlayerController playerController){
        SettingInfo playerSettingInfo = playerController.getPlayer().getExtInfo(SettingInfo.class);
        List<String> blackList = playerSettingInfo.getBlackList();
        SGPlayerProto.S2C_MailBlackList.Builder  b = SGPlayerProto.S2C_MailBlackList.newBuilder();
        if(blackList != null && blackList.size() > 0){
            List<PlayerModel> playerModelList = playerService.getPlayerListByIndex(blackList);
            SGCommonProto.BlackList.Builder bl  = null;
            for(PlayerModel playerModel : playerModelList){
                bl = SGCommonProto.BlackList.newBuilder();
                bl.setNickName(playerModel.getName());
                bl.setTargetIndex(playerModel.getObjectIndex());
                bl.setTargetAvatar(playerModel.getAvatar());
                bl.setCamp(playerModel.getCamp());
                bl.setLv( playerModel.getLevel());
                b.addBlackList(bl);
            }
        }
        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Mail_BlackList_VALUE, b.build().toByteArray());

    }

    /***
     * 加入移除黑名单
     * @param playerController
     */
    public void addAndRemoveBlackList(PlayerController playerController,String objectIndex,int type){
        SGPlayerProto.S2C_MailAddAndRemoveBlackList.Builder b = SGPlayerProto.S2C_MailAddAndRemoveBlackList.newBuilder();
        SettingInfo playerSettingInfo = playerController.getPlayer().getExtInfo(SettingInfo.class);

        if(type == SGCommonProto.E_BLACK_LIST_TYPE.BLACK_LIST_TYPE_ADD_VALUE){
            if(playerSettingInfo.isContains(objectIndex)){
                //黑名单列表已存在
                playerController.sendWarn(WordFactory.THIS_USER_HAS_A_LIST_OF_BLACKLISTS);
                return;
            }else {
                playerSettingInfo.addBlackList(objectIndex);
            }
        }else{
            if(!playerSettingInfo.isContains(objectIndex)){
                //黑名单列表不存在
                playerController.sendWarn(WordFactory.THIS_USER_IS_NOT_ON_A_LIST_OF_BLACKLISTS);
                return;
            }else {
                playerSettingInfo.removeBlackList(objectIndex);
            }
        }
        b.setTargetIndex(objectIndex);
        b.setType(SGCommonProto.E_BLACK_LIST_TYPE.forNumber(type));
        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Mail_AddAndRemoveBlackList_VALUE, b.build().toByteArray());

    }



    //设置邮件已读，并把附件物品加入背包
    public void setReadMail(MailDB mailDB, BagInfo bagInfo){
        if(!mailDB.isRead)
            mailDB.isRead = true;
        //奖励加入背包
        for(GoodsData goodsData : mailDB.getAttachList()){
            bagInfo.addGoods(goodsData);
        }
    }


    /***
     * 判断用户是否在线，如果在线列表中存在那么在在线列表中去，否则离线用户中获取
     * @param targetIndex
     * @return
     */
   /* public String  getAvatar(String targetIndex){
        String avatar = null;
        List<PlayerController> onlinePlayers = GameServer.getInstance().getWorldManager().getPlayerList();
        Player player = null;
        PlayerModel playerModel = null;
        for (PlayerController onlinePlayer : onlinePlayers){
            if(onlinePlayer.getPlayerResult().getAccount().equals(targetIndex)){
                avatar = onlinePlayer.getPlayerResult().avatar;
//                player = onlinePlayer.getPlayerResult();
                break;
            }
        }
        if (avatar == null || avatar.equals("")){
            playerModel = playerService.getPlayerByIndex(targetIndex);
            avatar = playerModel.getAvatar();
        }
        return avatar;
    }
*/



}
